package lore.miniwechat.security;

import lore.miniwechat.dao.UserDao;
import lore.miniwechat.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class LoreDetailService implements UserDetailsService {

    @Autowired
    UserDao dao;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("用户登录：" + username);

        User user = dao.findByName(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        UserDetails userDetails = org.springframework.security.core.userdetails.User.builder()
                .username(username)
                .password(user.getPassword())
                .authorities(AuthorityUtils.NO_AUTHORITIES)     // 设置一个空的权限
                .build();
        return userDetails;
    }
}
